﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ByteSite.Common;
using ByteSite.Quartz;
using System.Threading.Tasks;

namespace ByteSite.Web.admin.manager
{
    public partial class task_edit : Web.UI.ManagePage
    {
        private string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");
            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                if (!int.TryParse(Request.QueryString["id"] as string, out this.id))
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!new BLL.quartz_task().Exists(this.id))
                {
                    JscriptMsg("记录不存在或已被删除！", "back");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("task_list", DTEnums.ActionEnum.View.ToString()); //检查权限
                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }
        }

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.quartz_task bll = new BLL.quartz_task();
            Entitys.QuartzTask model = bll.GetModel(_id);

            txtName.Text = model.name;
            txtIntro.Text = model.intro;
            txtStartTime.Text = Utils.ObjectToDateTime(model.start_time).ToString("yyyy-MM-dd HH:mm:ss");
            txtEndTime.Text = Utils.ObjectToDateTime(model.end_time).ToString("yyyy-MM-dd HH:mm:ss");
            txtCron.Text = model.cron;
            ddlType.SelectedValue = model.type;
            rdlStatue.SelectedValue = model.status.ToString();

            txtAssemblyClassName.Text = model.assembly_classname;
            txtAssemblyName.Text = model.assembly_name;
            txtAssemblyParams.Text = model.assembly_params;

            txtUrl.Text = model.url;
            txtRequestHeader.Text = model.request_header;
            txtRequestParams.Text = model.request_params;
            ddlRequestType.SelectedValue = model.request_type;


        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            Entitys.QuartzTask model = new Entitys.QuartzTask();
            BLL.quartz_task bll = new BLL.quartz_task();

            model.name = txtName.Text;
            model.group = "default";
            model.intro = txtIntro.Text;
            model.start_time = Utils.ObjectToDateTime(txtStartTime.Text, DateTime.Now);
            model.end_time = Utils.ObjectToDateTime(txtEndTime.Text, DateTime.Parse("2099/12/31 23:59:59"));
            model.cron = txtCron.Text;
            model.type = ddlType.SelectedValue;
            model.status = Utils.ObjToInt(rdlStatue.SelectedValue);

            model.assembly_classname = txtAssemblyClassName.Text;
            model.assembly_name = txtAssemblyName.Text;
            model.assembly_params = txtAssemblyParams.Text;

            model.url = txtUrl.Text;
            model.request_header = txtRequestHeader.Text;
            model.request_params = txtRequestParams.Text;
            model.request_type = ddlRequestType.SelectedValue;

            model.delete_flag = 0;
            model.add_time = DateTime.Now;
            model.add_user = GetAdminInfo().id;
            model.update_time = DateTime.Now;
            model.update_user = GetAdminInfo().id;

            model.id = bll.Add(model);
            if (model.id > 0)
            {
                Task<string> s = QuartzHelper.GetJob(model.id.ToString(), model.group);

                QuartzHelper.AddJob(model).GetAwaiter().GetResult();
                if (model.status == 1)
                {
                    QuartzHelper.PauseJob(model.id.ToString(), model.group).GetAwaiter();
                }
                else if (model.status == 2)
                {
                    QuartzHelper.StartJob(model.id.ToString(), model.group).GetAwaiter();
                    //QuartzHelper.ResumeJob(model.id.ToString(), model.group).GetAwaiter();
                }
                return true;
            }
            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = false;
            BLL.quartz_task bll = new BLL.quartz_task();
            Entitys.QuartzTask model = bll.GetModel(_id);

            model.name = txtName.Text;
            model.group = "default";
            model.intro = txtIntro.Text;
            model.start_time = Utils.ObjectToDateTime(txtStartTime.Text, DateTime.Now);
            model.end_time = Utils.ObjectToDateTime(txtEndTime.Text, DateTime.Parse("2099/12/31 23:59:59"));
            model.cron = txtCron.Text;
            model.type = ddlType.SelectedValue;
            model.status = Utils.ObjToInt(rdlStatue.SelectedValue);

            model.assembly_classname = txtAssemblyClassName.Text;
            model.assembly_name = txtAssemblyName.Text;
            model.assembly_params = txtAssemblyParams.Text;

            model.url = txtUrl.Text;
            model.request_header = txtRequestHeader.Text;
            model.request_params = txtRequestParams.Text;
            model.request_type = ddlRequestType.SelectedValue;

            model.update_time = DateTime.Now;
            model.update_user = GetAdminInfo().id;

            if (bll.Update(model))
            {
                Task<string> s = QuartzHelper.GetJob(model.id.ToString(), model.group);
                string ss = s.Result;

                QuartzHelper.DeleteJob(model.id.ToString(), model.group).GetAwaiter();
                
                if (model.status == 2)
                {
                    QuartzHelper.AddJob(model).GetAwaiter().GetResult();
                    QuartzHelper.StartJob(model.id.ToString(), model.group).GetAwaiter();
                }

                //QuartzHelper.DeleteJob(model.id.ToString(), model.group).GetAwaiter().GetResult();
                //QuartzHelper.AddJob(model).GetAwaiter().GetResult();
                result = true;
            }

            return result;
        }
        #endregion

        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("task_list", DTEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("修改成功！", "task_list.aspx");
            }
            else //添加
            {
                ChkAdminLevel("task_list", DTEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("添加成功！", "task_list.aspx");
            }
        }

    }
}